package com.dpteam.util;

import com.dpteam.bean.*;
import com.dpteam.enums.Gender;
import com.dpteam.model.*;

import java.text.DecimalFormat;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/8
 * Time: 15:51
 */
public class TransformUtil {

    private static DecimalFormat df=new java.text.DecimalFormat("#.##");

    /**
     * 将User 转换为 UserInfo
     * @param user
     * @param withPassword 密码字段是否设值
     * @return
     */
    public static UserInfo fromUser(User user, boolean withPassword) {
        int genderCode = user.getGender();
        Gender gender = Gender.get(genderCode);
        Role role = user.getRole();
        UserInfo userInfo = new UserInfo();
        userInfo.setId(user.getId());
        userInfo.setName(user.getName());
        userInfo.setAccount(user.getAccount());
        userInfo.setEmail(user.getEmail());
        userInfo.setGender(gender.getNote());
        userInfo.setOnJobTime(user.getOnJobTime().toLocalDate().toString());
        userInfo.setGenderCode(gender.getCode());
        userInfo.setRoleName(role.getRoleType());
        userInfo.setRoleId(role.getId());
        userInfo.setPhone(user.getPhone());
        userInfo.setImgPath(user.getImgPath());
        if (withPassword) {
            userInfo.setPassword(user.getPassword());
        }
        return userInfo;
    }

    /**
     * 将User 转换为 UserInfo
     * @param user
     * @return
     */
    public static UserInfo fromUser(User user) {
        return fromUser(user, true);
    }

    /**
     * 将ImportProduct 转换为 ImportInfo
     * @param importProduct
     * @return
     */
    public static ImportInfo fromImportProduct(ImportProduct importProduct) {
        ImportInfo importInfo = new ImportInfo();
        Product product = importProduct.getProduct();
        User user = importProduct.getUser();
        Provider provider = product.getProvider();
        Category category = product.getCategory();

        importInfo.setId(importProduct.getId());
        importInfo.setAmount(importProduct.getAmount());
        importInfo.setUpdateTime(importProduct.getUpdateTime().toLocalDate().toString());
        importInfo.setTotal(Float.parseFloat(df.format(importProduct.getTotal())));

        importInfo.setUser(TransformUtil.fromUser(user, false));

        importInfo.setBarcode(product.getBarcode());
        importInfo.setProductId(product.getId());
        importInfo.setProductName(product.getProductName());
        importInfo.setBuyPrice(Float.parseFloat(df.format(product.getBuyPrice())));
        if (provider != null) {
            importInfo.setProviderName(provider.getProviderName());
            importInfo.setProviderDes(provider.getProviderDes());
        }

        importInfo.setTypeId(category.getId());
        importInfo.setTypename(category.getTypeName());

        return importInfo;
    }

    /**
     * 将Member 转换为 MemberInfo
     * @param member
     * @return
     */
    public static MemberInfo fromMember(Member member) {
        MemberInfo memberInfo = new MemberInfo();
        MemberLevel memberLevel = member.getMemberLevel();
        Gender gender = Gender.get(member.getGender());
        User user = member.getUser();
        memberInfo.setId(member.getId());
        memberInfo.setName(member.getName());
        memberInfo.setPhone(member.getPhone());
        memberInfo.setIDCard(member.getIDCard());
        memberInfo.setIntegral(member.getIntegral());
        memberInfo.setAllIntegral(member.getAllIntegral());
        memberInfo.setGenderCode(gender.getCode());
        memberInfo.setGenderNote(gender.getNote());
        memberInfo.setRegisttime(member.getRegisttime().toLocalDate().toString());
        if (memberLevel != null) {
            memberInfo.setMemberLevelId(memberLevel.getId());
            memberInfo.setLevelCode(memberLevel.getLevel());
            memberInfo.setLevelNote(memberLevel.getNote());
        }
        memberInfo.setCreateBy(user.getName());
        return memberInfo;
    }

    /**
     * 将OrderItem 转换为 OrderItemInfo
     * @param orderItem
     * @return
     */
    public static OrderItemInfo fromOrderItem(OrderItem orderItem) {
        OrderItemInfo orderItemInfo = new OrderItemInfo();
        Product product = orderItem.getProduct();

        orderItemInfo.setId(orderItem.getId());
        orderItemInfo.setProductId(product.getId());
        orderItemInfo.setBarcode(product.getBarcode());
        orderItemInfo.setProductName(product.getProductName());
        orderItemInfo.setTypeName(product.getCategory().getTypeName());
        orderItemInfo.setDescription(product.getDescription());
        orderItemInfo.setProductNum(orderItem.getProductNum());
        orderItemInfo.setDiscount(orderItem.getDiscount());
        orderItemInfo.setProfitTotal(Float.parseFloat(df.format(orderItem.getProfitTotal())));
        orderItemInfo.setSellPriceTotal(Float.parseFloat(df.format(orderItem.getSellPriceTotal())));
        orderItemInfo.setPerSellPrice(Float.parseFloat(df.format(orderItem.getPerSellPrice())));
        return orderItemInfo;
    }

    /**
     * 将RejectOrder 转换为 RejectOrderInfo
     * @param rejectOrder
     * @return
     */
    public static RejectOrderInfo fromRejectOrder(RejectOrder rejectOrder) {
        RejectOrderInfo rejectOrderInfo = new RejectOrderInfo();
        Order order = rejectOrder.getOrder();
        Product product = rejectOrder.getProduct();
        User user = rejectOrder.getUser();

        rejectOrderInfo.setId(rejectOrder.getId());
        rejectOrderInfo.setOrderid(order.getId());
        rejectOrderInfo.setUserId(user.getId());
        rejectOrderInfo.setReason(rejectOrder.getRejectReason().getReason());
        rejectOrderInfo.setTime(rejectOrder.getTime().toLocalDate().toString());
        rejectOrderInfo.setProductId(product.getId());
        rejectOrderInfo.setProductName(product.getProductName());
        rejectOrderInfo.setNumber(rejectOrder.getAmount());

        return rejectOrderInfo;
    }

    /**
     * ProductInfo 转换为 sellProductInfo
     * @param productInfo
     * @return
     */
    public static sellProductInfo fromProductInfo(ProductInfo productInfo) {
        sellProductInfo sellProductInfo = new sellProductInfo();
        sellProductInfo.setProductId(productInfo.getId());
        sellProductInfo.setProductName(productInfo.getProductName());
        sellProductInfo.setSetPrice(Float.parseFloat(df.format(productInfo.getSetPrice())));
        return sellProductInfo;
    }

    /**
     * 将News 转换为NoticeInfo
     * @param news
     * @return
     */
    public static NoticeInfo fromNews(News news) {
        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setId(news.getId());
        noticeInfo.setTitle(news.getTitle());
        noticeInfo.setMessage(news.getMessage());
        noticeInfo.setPublishTime(news.getPublishTime().toLocalDate().toString());
        noticeInfo.setPublisher(news.getUser().getName());
        return noticeInfo;
    }

    /**
     * Product 转换为 ProductInfo
     * @param product
     * @return
     */
    public static ProductInfo fromProduct(Product product) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setId(product.getId());
        productInfo.setBarcode(product.getBarcode());
        productInfo.setBuyPrice(Float.parseFloat(df.format(product.getBuyPrice())));
        productInfo.setCategoryId(product.getCategory().getId());
        productInfo.setProductName(product.getProductName());
        productInfo.setSetPrice(Float.parseFloat(df.format(product.getSetPrice())));
        productInfo.setStorage(product.getStorage());
        if(product.getProvider() != null)
            productInfo.setProviderId(product.getProvider().getId());
        productInfo.setDescription(product.getDescription());
        return productInfo;
    }
}
